﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using CapaEntidades;


namespace CapaDatos
{
    public class daLinea
    {
        #region singleton
        private static readonly daLinea _instancia = new daLinea();
        public static daLinea Instancia
        {
            get { return daLinea._instancia; }
        }
        #endregion

        #region Métodos        
        public enAlerta spRecepcionarLineaXML(String xml)
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            enAlerta lstProducto = new enAlerta();
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spRecepcionarLineaXML", cn);
                cmd.Parameters.AddWithValue("@prmXML", xml);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lstProducto.val = -1;
                if (dr.Read())
                {

                    lstProducto.val = Convert.ToInt16(dr[0].ToString());
                    lstProducto.msg = dr[1].ToString();

                }
            }
            catch (Exception)
            {
                lstProducto.val = -1;
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lstProducto;

        }

        public List<enLinea> spListaLineas()
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            List<enLinea> lst = null;
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spListaLineas", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lst = new List<enLinea>();
                while (dr.Read())
                {
                    enLinea l = new enLinea();
                    l.lin_id = Convert.ToInt16(dr[0].ToString());
                    l.lin_descripcion = dr[1].ToString();
                    l.comision = Convert.ToDouble(dr[2].ToString());
                    lst.Add(l);

                }
            }
            catch (Exception)
            {
                lst = null;
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lst;

        }

        public enAlerta spActualizarComision(enLinea l)
        {
            SqlConnection cn = null;
            SqlCommand cmd = null;
            SqlDataReader dr = null;
            enAlerta lstProducto = new enAlerta();
            try
            {

                cn = Conexion.getConexion();
                cmd = new SqlCommand("spActualizarComision", cn);
                cmd.Parameters.AddWithValue("@linea_id", l.lin_id);
                cmd.Parameters.AddWithValue("@comLin_monto", l.comision);
                cmd.CommandType = CommandType.StoredProcedure;
                cn.Open();
                dr = cmd.ExecuteReader();
                lstProducto.val = -1;
                if (dr.Read())
                {

                    lstProducto.val = Convert.ToInt16(dr[0].ToString());
                    lstProducto.msg = dr[1].ToString();

                }
            }
            catch (Exception)
            {
                lstProducto.val = -1;
                throw;
            }
            finally
            {
                dr.Close();
                cn.Close();
            }
            return lstProducto;

        }
        #endregion

    }
}
